<?php
namespace app\admin\controller;
use app\model\User as UserModel;
use app\model\Admin as AdminModel;
use think\Db;


class User extends Base
{
    public function __construct(){
        $model = new UserModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '用户';

        if ( in_array($page_info['action'], ['add','edit']) ) {
            $admins = AdminModel::mylist();
            $admins = arrayParamExtract($admins,'id','account');

            $fields = [
                'account'=>['name'=>'用户名','type'=>'text','valids'=>['required']],
                'password'=>['name'=>'密码','type'=>'text','valids'=>['required']],
                'sign'=>['name'=>'秘钥','type'=>'text','valids'=>['required']],
                'truename'=>['name'=>'姓名','type'=>'text'],
                'mobile'=>['name'=>'手机号码','type'=>'text'],
                'played_excess_balance'=>['name'=>'已打款授信','type'=>'text'],
                'excess_balance'=>['name'=>'未打款授信','type'=>'text'],
                'ip_address'=>['name'=>'IP白名单','type'=>'text','placeholder'=>'空，不做IP检验'],
                'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
                'is_realtime'=>['name'=>'实时回调','type'=>'select1','params'=>$model->getCommonIsList()],
                'is_realtime_submit'=>['name'=>'实时提交','type'=>'select1','params'=>$model->getCommonIsList()],
                'is_switch_gallery'=>['name'=>'切换通道','type'=>'select1','params'=>$model->getCommonIsList()],
                'switch_gallery_time'=>['name'=>'切换通道有效时间（分钟）','type'=>'select1','params'=>$model->getSwitchList()],
                // 'is_poll'=>['name'=>'轮询通道','type'=>'select1','params'=>$model->getCommonIsList()],
                // 'group_synchro'=>['name'=>'同步通道组折扣','type'=>'select1','params'=>$model->getCommonIsList()],
                // 'group_status'=>['name'=>'通道组折扣变更维护','type'=>'select1','params'=>$model->getCommonIsList()],
                'admin_id'=>['name'=>'所属管理员','type'=>'select1_1','params'=>$admins],

            ];
            if ($page_info['action'] == 'edit') {
                $fields['password']['placeholder'] = '留空，则不修改';
                $fields['password']['value'] = '';
                unset($fields['password']['valids']);
            }

            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids
            ]);
        }

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'account','type'=>'like'],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        
        $search_params = [
            [
                ['key'=>'account','name'=>'用户名','type'=>'text'],
                ['key'=>'truename','name'=>'姓名','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()]
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        return parent::index($map);
    }

    public function export(){
        $model = $this->model;
        $map_params = [
            ['key'=>'account','type'=>'like'],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        
        $list = $model->where($map)->order('id','desc')->select();
        $data = [];
        $title = ['用户名','真实姓名','账户余额','已打款授信余额','未打款授信余额','IP地址','自动找全国','能跑限价','状态','所属销售','创建时间'];
        foreach ($list as $k => $vo) {
            $tmp = [
                exportReplace($vo['account']),
                exportReplace($vo['truename']),
                $vo['balance'],$vo['played_excess_balance'],$vo['excess_balance'],
                exportReplace($vo['ip_address']),
                $model->getCommonIsCn($vo['auto_quanguo']),
                $model->getCommonIsCn($vo['go_fixed']),
                $model->getStatusCn($vo['status']),
                Db::name('admin')->where('id',$vo['admin_id'])->value('truename'),
                timeDef($vo['ctime'])
            ];
            $data[] = $tmp;
        }
        exportCsv(date('YmdHis'),$title,$data);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['account','password','sign'],
                'nomust'=>['truename','mobile','played_excess_balance','excess_balance','ip_address','status','is_realtime','is_realtime_submit','is_switch_gallery','is_poll','group_synchro','group_status','admin_id']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $save_data['password'] = md5($save_data['password']);
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'nomust'=>['account','password','sign','truename','mobile','played_excess_balance','excess_balance','ip_address','status','is_realtime','is_realtime_submit','is_switch_gallery','is_poll','group_synchro','group_status','admin_id']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['password'])) {
                $save_data['password'] = md5($save_data['password']);
            }else{
                unset($save_data['password']);
            }
        }
        return parent::updateData($id,$save_data);
    }

    public function excess(){
        $model = $this->model;
        $id = input('user_id');
        if (empty($id)) { $this->error('id不能为空'); }
        $data = $model->where('id',$id)->find();

        if (request()->isPost()) {
            $fields = [
                'must'=>['played_excess_balance','excess_balance']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $save_data['balance_remind'] = $save_data['excess_balance']+$save_data['played_excess_balance']+$data['balance'];
            if ( $model->where('id',$id)->update($save_data) !== false ) {
                $this->success("编辑成功",url('index'));
            }else{
                $this->error("编辑失败");
            }
        }
    }



   
}
